/**
 * 
 */
package org.kopsox.util.persistence.impl;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.kopsox.comm.message.ErrorMessage;
import org.kopsox.comm.message.impl.ErrorMessageImpl;
import org.kopsox.data.container.ValueContainer;
import org.kopsox.data.impl.Polymorph;
import org.kopsox.general.priority.PriorityEnum;
import org.kopsox.general.type.ErrorTypeEnum;
import org.kopsox.util.persistence.PersistenceDelegate;

/**
 * This implementation of the PersistenceDelegate persists ValueContainers as XML-Files or loads ValueContainers from XML-Files<br>
 * 
 * @author Konrad Renner - 17.11.2010
 *
 */
public class ContainerXMLPersistenceDelegate implements PersistenceDelegate {
	
	private static final long serialVersionUID = 404278888378937001L;
	
	private final String path;
	
	public ContainerXMLPersistenceDelegate(String pathToXml) {
		this.path = pathToXml;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.persistence.PersistenceDelegate#loadInto(java.lang.Object)
	 */
	@Override
	public List<ErrorMessage> loadInto(Object data)throws KopsoxPersistenceException{
		List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
		if(data instanceof ValueContainer) {
			ValueContainer container = (ValueContainer)data;
			
			try {
				FileInputStream fos = new FileInputStream(path);
	            ObjectInputStream oos = new ObjectInputStream(fos);
				
				//TODO load the data correct
				XMLDecoder enc = new XMLDecoder(oos);
	            Object o = enc.readObject();
	            container.setValue("object", new Polymorph<Serializable>((Serializable)o));
	            enc.close();
			} catch (FileNotFoundException e) {
				throw new KopsoxPersistenceException(e);
			} catch (IOException e) {
				throw new KopsoxPersistenceException(e);
			}
		}else {
			errors.add(new ErrorMessageImpl(PriorityEnum.HIGHER.getPriority(), ErrorTypeEnum.FALSETYPE));
		}
		
		return errors;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.persistence.PersistenceDelegate#persistFrom(java.lang.Object)
	 */
	@Override
	public List<ErrorMessage> persistFrom(Object data)throws KopsoxPersistenceException {
		List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
		if(data instanceof ValueContainer) {
			ValueContainer container = (ValueContainer)data;
			
			try {
				FileOutputStream fos = new FileOutputStream(path);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				
				//TODO write the data correct
				XMLEncoder enc = new XMLEncoder(oos);
                enc.writeObject(container);
                enc.close();
			} catch (FileNotFoundException e) {
				throw new KopsoxPersistenceException(e);
			} catch (IOException e) {
				throw new KopsoxPersistenceException(e);
			}
		}else {
			errors.add(new ErrorMessageImpl(PriorityEnum.HIGHER.getPriority(), ErrorTypeEnum.FALSETYPE));
		}
		
		return errors;
	}

}
